<template>
  <div>
    <PageDialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <div class="dialog-table">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
        >
          <template v-slot:filter-button>
            <div class="dialog-table-header">
              <div class="header-left">
                <div class="lable">产品名称：{{ info.commonName }}</div>
                <div class="lable">生产批号：{{ info.batchNumber }}</div>
              </div>
              <div class="header-right">
                <div class="scan-groups">
                  <el-input
                    v-model="filterInfo.keyword"
                    :clearable="true"
                    placeholder="请输入个体码"
                    @keyup.enter.native="search"
                    @input="(e) => (filterInfo.keyword = validSe(e))"
                  ></el-input>
                  <el-button style="margin-left: 10px" @click="search"
                    >搜索</el-button
                  >
                </div>
              </div>
            </div>
          </template>
        </NewPageFilter>
      </div>
      <el-form :model="tableInfo" ref="tableForm" style="width: 100%">
        <DialogTable
          :refresh="tableInfo.refresh"
          :init-curpage="tableInfo.initCurpage"
          v-model:data="tableInfo.data"
          :api="tableInfo.api"
          :check-box="false"
          :tab-index="true"
          :listen-height="false"
          :pager="false"
          v-model:query="filterInfo.query"
          :field-list="tableInfo.fieldList"
          :handle="tableInfo.handle"
          @handleEvent="handleEvent"
        >
        </DialogTable>
      </el-form>
    </PageDialog>
    <ChosePrinter :visible="printerInfo.visible" @callBack="printerCallBack" />
  </div>
</template>

<script setup name="IndividualDetails">
import {
  getDepotStockQueryCustomizeList,
  depotStockDetailsPrintQrCode,
  depotStockPrintQrCode,
} from '@/api/dataQuery.js'

import { getCustomizeSetting } from '@/api/customize'
const { proxy } = getCurrentInstance()
const props = defineProps({
  api: {
    type: Function,
    default: getDepotStockQueryCustomizeList,
  },
  showDialog: {
    type: Boolean,
  },
  pageStatus: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  info: {
    type: Object,
    default: function () {
      return {}
    },
  },
})
const selectedScanValue = ref('1')
const selectRow = ref('')
const scanInput = ref('')

// 过滤相关配置
const filterInfo = ref({
  keyword: '',
  query: {
    curPage: 1,
    pageSize: 99999,
    queryMap: {
      ...props.query,
      depotIdAuth: {
        asTbName: 'depot_stock',
        columnName: 'depot_id',
        queryType: 'IN',
        queryCategory: 'AND',
        value: 'depotAuth',
      },
    },
    customizeUserVO: {},
  },
  list: [{ slot: true, value: 'button' }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  copyData: [],
  api: undefined,
  selections: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '打印二维码',
        event: printSingleQrCode,
        show: true,
      },
    ],
  },
})
// 弹窗相关
const dialogInfo = ref({
  title: '个体详情',
  btLoading: false,
  btList: [{ label: '关闭', event: handleClose, show: true }],
})
const printerInfo = ref({
  visible: false,
  data: {},
})
const shows = computed({
  get() {
    return props.showDialog
  },
  set(val) {
    proxy.$emit('update:showDialog', val)
  },
})
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  const search = filterInfo.value.keyword
  if (search) {
    tableInfo.value.data = tableInfo.value.copyData.filter((data) => {
      return Object.keys(data).some((key) => {
        if (key === 'qrCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'uniqueCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'factorySubCode') {
          return String(data[key]).indexOf(search) > -1
        } else if (key === 'factoryMainCode') {
          return String(data[key]).indexOf(search) > -1
        }
      })
    })
  } else {
    tableInfo.value.data = tableInfo.value.copyData
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  proxy.$emit('reset')
}
function handleSave() {
  handleClose()
}
function handleEvent(event, data) {
  switch (event) {
    case 'list':
      tableInfo.value.copyData = data
      break
  }
}
//打印单个二维码
function printSingleQrCode(row = undefined) {
  if (!row.qrCode) {
    proxy.$message({
      message: '没有生成二维码，不能打印',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  let ids = [row.id]
  if (ids.length > 0) {
    depotStockDetailsPrintQrCode(ids).then((resp) => {
      if (resp && resp.success) {
        printerInfo.value.visible = true
        printerInfo.value.data = resp.data
      }
    })
  }
}
//打印整单的二维码
function pringQrCode() {
  let ids = []
  ids.push(tableRow.value.id)
  if (ids.length > 0) {
    depotStockPrintQrCode(ids).then((resp) => {
      if (resp && resp.success) {
        printerInfo.value.visible = true
        printerInfo.value.data = resp.data
      }
    })
  }
}
function printerCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.value.data, printer)
  }
  printerInfo.value.visible = false
}
function getCustomize() {
  let param = {
    tableKey:
      'consumablesDepotStockIndividualDetails-com.slip.spd.entity.vo.stock.DepotStockVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      tableInfo.value.api = props.api
      getList()
    }
  })
}

let obj = {
  price: {
    label: '单价',
    value: 'price',
    formatter: proxy.$fn.TripartiteMethod,
    type: 'column',
    componentType: 'price',
    permission: !proxy.$permission('depotStockQuery:price'),
    hidden: true,
    show: false,
  },
  sumAmount: {
    label: '总金额',
    value: 'sumAmount',
    formatter: proxy.$fn.TripartiteMethod,
    type: 'column',
    componentType: 'price',
    permission: !proxy.$permission('depotStockQuery:price'),
    hidden: true,
    show: false,
  },
  defaultSupplierPrice: {
    label: '默认供应商价格',
    value: 'defaultSupplierPrice',
    type: 'column',
    componentType: 'price',
    permission: !proxy.$permission('depotStockQuery:price'),
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
    show: false,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()
</script>

<style scoped lang="scss">
:deep(.filter-item) {
  width: 100%;
}
.text-style {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.scan-groups {
  display: flex !important;
  align-items: center;
}
</style>
